package com.situ.root.goodsB.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;


import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.situ.root.goodsB.model.PictureModel;
import com.situ.root.goodsB.service.IPictureService;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("Picture")
public class PictureConreoller {

	@Autowired
	private IPictureService pictureService;
	
	private String PATH = "C:/Users/14622/Desktop/3/picture/";
	
	@RequestMapping("/fileUpload.do")  
	@ResponseBody
    public String fileUpload(HttpServletRequest request,String code) throws IllegalStateException, IOException{  
        long startTime=System.currentTimeMillis();   //获取开始时间  
          
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
        if(multipartResolver.isMultipart(request)){ //判断request是否有文件上传  
        	//将Request变成多部分Request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
            //获取multiRequest中所有的文件名
            Iterator<String> ite = multiRequest.getFileNames();  
            //一次性遍历所有文件
            while(ite.hasNext()){  
                MultipartFile file = multiRequest.getFile(ite.next());  
//                if(file!=null){  
//                    File localFile = new File("C:\\Users\\14622\\Desktop\\3\\picture"+file.getOriginalFilename());  
//                    try {  
//                        file.transferTo(localFile); //将上传文件写到服务器上指定的文件  
//                    } catch (IllegalStateException e) {  
//                        e.printStackTrace();  
//                    } catch (IOException e) {  
//                        e.printStackTrace();  
//                    }  
//                }
                if(file==null)
                	continue;
                String ofn = file.getOriginalFilename();
                String nfn = UUID.randomUUID().toString() + ofn.substring(ofn.lastIndexOf('.'));
                String path = PATH +nfn;
                file.transferTo(new File(path));
                pictureService.insert(new PictureModel(code,nfn,"2"));
            }
            
        }  
        long endTime=System.currentTimeMillis(); //获取结束时间  
        System.out.println("上传文件共使用时间："+(endTime-startTime));  
        Map<String, String> result = new HashMap<>();
        result.put("code", "0");
        return net.sf.json.JSONObject.fromObject(result).toString();
//        return "success"; 
	}
	
	
	@RequestMapping(value = "/get.do", produces={"application/json;charset=UTF-8"},method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
//	public PictureModel get(Object model) {
//		PictureModel res = pictureService.selectId(model);
//		
//		System.out.println("res+" + res);
//		return res;
//	}
	public List<PictureModel> get(PictureModel model){
		List<PictureModel> res = pictureService.selectList(model);
		res = JSONArray.fromObject(res);
		System.out.println("+++"+res);
		return res;
	}
}
